from db2 import Db,Primary,Foreign,String

class Problemreportdb(Db):
    """
    """
 
    def define(self):
        """
        """
        self.add_table('user',
                       uname=Primary(String()),
                       uability=Foreign('ability')
                       )

        self.add_table('role',
                       rname=Primary(String())
                       )

        self.add_table('ability',
                       aname=Primary(String())
                       )

        self.add_table('interest',
                       iuser = Foreign('user'),
                       irole = Primary(Foreign('role')),
                       iissue = Primary(Foreign('issue'))
                       )
                       
        self.add_table('issue',
                       title=Primary(String()),
                       istatus=Foreign('status'),
                       ipriority=Foreign('priority')
                      )

        self.add_table('message',
                       text=Primary(String()),
                       missue = Foreign('issue')
                       )
        
        self.add_table('status',
                       sname=Primary(String())
                       )
        
        self.add_table('priority',
                       pname=Primary(String())
                       )
        
        self.add_table('keywords',
                       kname=Primary(String())
                       )

        self.add_table('keyword',
                       kword=Primary(Foreign('keywords')),
                       kissue=Primary(Foreign('issue'))
                       )

    def populate(self):
        """
        """
        for a in ('admin', 'user'):
           self.ability.append(aname=a)
        for r in ('author', 'editor', 'assigned'):
           self.role.append(rname=r)
        for p in ('high', 'medium', 'low', 'wish'):
           self.priority.append(pname=p)
        for s in ('open', 'fixed', 'noproblem', 'deferred'):
           self.status.append(sname=s)
        for u, a in (('steve.white', 'admin'), ('carl.hesketh', 'user')):
           self.user.append(uname=u, uability=self.ability.find(aname=a).key)
        for k in ('db2', 'stx', 'prdb'):
           self.keywords.append(kname=k)
           
if __name__ == '__main__':
    import sys
    if len(sys.argv)>1:
        from db2.db2tk import DbApp
    else:
        from db2.db2ch import DbApp
    DbApp(Problemreportdb())
